package com.xuanyue.db.xuan.core.table.groupby;

import java.io.Closeable;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.concurrent.locks.ReentrantReadWriteLock;

public class GroupKeyWriteAndReader implements Closeable{

	private RandomAccessFile rws = null;//new RandomAccessFile(String.format("%s/xyy.sun", path),"r");
	private ReentrantReadWriteLock rwLock = new ReentrantReadWriteLock();
	public GroupKeyWriteAndReader(String path)throws IOException{
		rws = new RandomAccessFile(String.format("%s/xyy.sun", path),"rws");
	}
	
	public void writeInt(int rowId,int value) throws IOException {
		rws.seek(rowId*4);
		rws.writeInt(value);
	}
	public void writeLong(int rowId,long value) throws IOException {
		rws.seek(rowId*8);
		rws.writeLong(value);
	}
	public int readInt(int rowId)throws IOException  {
		rws.seek(rowId*4);
		return rws.readInt();
	}
	public long readLong(int rowId)throws IOException  {
		rws.seek(rowId*4);
		return rws.readLong();
	}
	@Override
	public void close() throws IOException {
		rws.close();
	}
	public static void main(String[] args) {
		ReentrantReadWriteLock rwLock = new ReentrantReadWriteLock();
		long now = System.currentTimeMillis();
		int x =0;
		for(int i=0;i<100000;i++) {
			rwLock.writeLock().lock();
			x++;
			rwLock.writeLock().unlock();
		}
		System.out.println(x);
		System.out.println(System.currentTimeMillis() - now);
	}
}
